package com.xinsoft.validator;

import cn.hutool.core.util.StrUtil;
import com.xinsoft.common.exception.BDException;
import com.xinsoft.exception.MyException;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;

/**
 * hibernate-validator校验工具类
 *
 * @author Li
 * @version 1.0.0
 * @createdate 2019/1/2
 */
public class ValidatorUtils {
    private static Validator validator;

    static {
        validator = Validation.buildDefaultValidatorFactory().getValidator();
    }

    /**
     * 校验对象
     *
     * @param object 待校验对象
     * @param groups 待校验的组
     * @throws MyException 校验不通过，则报MyException异常
     */
    public static void validateEntity(Object object, Class<?>... groups)
            {
        final String separator = "、";
        Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups);
        if (!constraintViolations.isEmpty()) {
            StringBuilder msg = new StringBuilder();
            for (ConstraintViolation<Object> constraint : constraintViolations) {
                msg.append(constraint.getMessage()).append(separator);
            }
            msg.deleteCharAt(msg.length() - 1);
            throw new BDException(msg.toString(), 1000028);
        }
    }

    /**
     * 校验实体对象,并返回首条错误信息
     *
     * @param object 对象实体
     *               groups 组类数组 例子 AddGroup UpdateGroup
     * @throws MyException 自定义异常类
     * @author: chenzh
     * @date 2021/3/15 9:22
     **/
    public static void validateObject(Object object, Class<?>... groups) {
        validateObject(object, null, groups);
    }

    /**
     * 校验实体对象,并返回首条错误信息
     *
     * @param object     对象实体
     * @param groups     组类数组 例子 AddGroup UpdateGroup
     * @param objectName 对象实体名称
     * @throws MyException 自定义异常类
     * @author: chenzh
     * @date 2021/3/15 9:22
     **/
    public static void validateObject(Object object, String objectName, Class<?>... groups) {
        Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups);
        if (!constraintViolations.isEmpty()) {
            for (ConstraintViolation<Object> constraint : constraintViolations) {
                StringBuilder stringBuilder = new StringBuilder();
                if (!StrUtil.isEmpty(objectName)) {
                    stringBuilder.append(objectName).append(":");
                }
                stringBuilder.append(constraint.getMessage());
                throw new BDException(stringBuilder.toString());
            }
        }
    }

    /**
     * 校验Excel对象
     *
     * @description
     * @param object:
     * @param groups:
     * @author Uyuhz
     * @date 2021/7/19 17:19
     * @return java.lang.String
     */
    public static String validateExcel(Object object, Class<?>... groups) {
        Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups);
        StringBuilder msg = new StringBuilder();
        if (!constraintViolations.isEmpty()) {
            for (ConstraintViolation<Object> constraint : constraintViolations) {
                msg.append(constraint.getMessage()).append("；");
            }
        }
        return msg.toString();
    }
}
